﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace bd2wpf
{
    /// <summary>
    /// Логика взаимодействия для Search.xaml
    /// </summary>
    public partial class Search : Window
    {
        int index=0;
        private MainWindow parent;
        public void setparent(MainWindow link)//функция получающая ссылку на родителя
        {
            parent = link;
        }
        
        public Search()
        {
            InitializeComponent();
        }

        private int SearchGo(int buf) //Поиск по записи
        {
            int n = MainWindow.dataSourceMyClass.myClasses.Count;
            if (SearchCB.Text == "Фамилии")
            {
                for (int i = buf; i < n; i++)
                {
                    if (parent.Check(MainWindow.dataSourceMyClass.myClasses[i].LastName, textSearch.Text)) { parent.dataGrid1.SelectedIndex = i; return i+1; }
                    
                }
                
            }
            else if (SearchCB.Text == "Имени")
            {
                for (int i = buf; i < n; i++)
                {
                    if (parent.Check(MainWindow.dataSourceMyClass.myClasses[i].Name, textSearch.Text)) { parent.dataGrid1.SelectedIndex = i; return i + 1; }
                }
            }
            else if (SearchCB.Text == "Отчеству")
            {
                for (int i = buf; i < n; i++)
                {
                    if (parent.Check(MainWindow.dataSourceMyClass.myClasses[i].Patronymic, textSearch.Text)) { parent.dataGrid1.SelectedIndex = i; return i + 1; }
                }
            }
            else if (SearchCB.Text == "Полу")
            {
                for (int i = buf; i < n; i++)
                {
                    if (parent.Check(MainWindow.dataSourceMyClass.myClasses[i].SexString, textSearch.Text)) { parent.dataGrid1.SelectedIndex = i; return i + 1; }
                }
            }
            else if (SearchCB.Text == "Моб.Тел.")
            {
                for (int i = buf; i < n; i++)
                {
                    if (parent.Check(MainWindow.dataSourceMyClass.myClasses[i].Mobily, textSearch.Text)) { parent.dataGrid1.SelectedIndex = i; return i + 1; }
                }
            }
            else if (SearchCB.Text == "Раб.Тел.")
            {
                for (int i = buf; i < n; i++)
                {
                    if (parent.Check(MainWindow.dataSourceMyClass.myClasses[i].Internal, textSearch.Text)) { parent.dataGrid1.SelectedIndex = i; return i + 1; }
                }
            }
            else if (SearchCB.Text == "E-mail")
            {
                for (int i = buf; i < n; i++)
                {
                    if (parent.Check(MainWindow.dataSourceMyClass.myClasses[i].EMail, textSearch.Text)) { parent.dataGrid1.SelectedIndex = i; return i + 1; }
                }
            }
            else if (SearchCB.Text == "Городу")
            {
                for (int i = buf; i < n; i++)
                {
                    if (parent.Check(MainWindow.dataSourceMyClass.myClasses[i].City, textSearch.Text)) { parent.dataGrid1.SelectedIndex = i; return i + 1; }
                }
            }
            else if (SearchCB.Text == "Улице")
            {
                for (int i = buf; i < n; i++)
                {
                    if (parent.Check(MainWindow.dataSourceMyClass.myClasses[i].Street, textSearch.Text)) { parent.dataGrid1.SelectedIndex = i; return i + 1; }
                }
            }
            else if (SearchCB.Text == "Должности")
            {
                for (int i = buf; i < n; i++)
                {
                    if (parent.Check(MainWindow.dataSourceMyClass.myClasses[i].Position, textSearch.Text)) { parent.dataGrid1.SelectedIndex = i; return i + 1; }
                }
            }
            else if (SearchCB.Text == "Подразделению")
            {
                for (int i = buf; i < n; i++)
                {
                    if (parent.Check(MainWindow.dataSourceMyClass.myClasses[i].Subdivision, textSearch.Text)) { parent.dataGrid1.SelectedIndex = i; return i + 1; }
                }
            }
            MessageBox.Show("Ничего не найдено."); 
            return 0;
        }

        private void Cansel_Click(object sender, RoutedEventArgs e)//отмена поиска
        {
            this.Close();
        }

        private void SearchB_Click(object sender, RoutedEventArgs e)//Начало поиска
        {
            index = 0;
            if (SearchCB.Text == "") { MessageBox.Show("Ошибка! Не выбранно поле поиска!"); }
            else {index = SearchGo(index); }
        }

        private void SearchB_Next_Click(object sender, RoutedEventArgs e)//Искать далее
        {
            if (SearchCB.Text == "") { MessageBox.Show("Ошибка! Не выбранно поле поиска!"); }
            else { index = SearchGo(index); }
        }     
    }
}
